//
//  StatusCell.swift
//  波波微博
//
//  Created by wave on 15/8/1.
//  Copyright © 2015年 wave. All rights reserved.
//

import UIKit

//设置行间距
let statusCellControlMargin: CGFloat = 8.0


//微博Cell的标识符枚举
enum StatusCellIdentifier: String{
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    //静态函数
    //根据微博数据,返回对应的标识符
    static func cellId(status:Status) -> String{
        
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusCell: UITableViewCell {

    //MARK: - cell中的top界面
    //cell中写入一个微博数据模型
    var status:Status?{
        didSet{
        
            topView.status = status
            
            //设置微博内容
            contentLabel.text = status?.text ?? ""
            
            //测试修改picture的高度
            pictureView.status = status
            print(pictureView.bounds.size)
            
            //设置配图视图的尺寸
            pictureHeightCons?.constant = pictureView.bounds.size.height
            pictureWidthCons?.constant = pictureView.bounds.size.width
            pictureTopCons?.constant = pictureView.bounds.size.height == 0 ? 0 : statusCellControlMargin
        }
    }
    
    //定义两个变量用来设置图片的宽度和高度的约束
    var pictureWidthCons: NSLayoutConstraint?
    var pictureHeightCons:NSLayoutConstraint?
    //图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    //计算行高
    func rowHeight(status:Status) -> CGFloat{
    
        //设置属性
        self.status = status
        
        //强行更新布局  -所有的控件的frame都会发生变化
        layoutIfNeeded()
        
        //返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //MARK: - 界面设置
    override init(style:UITableViewCellStyle,reuseIdentifier:String?){
        super.init(style:style,reuseIdentifier:reuseIdentifier)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //因为StatusForwarCell类要重写setupUI方法,所以需要将private去掉
    func setupUI(){
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)

        //设置布局
        //设置顶部视图自动布局
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        //内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset:CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        
        //宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))
        
        /*
            这里原来有配图视图的自动布局,现在封装到StatusNomalCell类中
        */
        
        //底部约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    
    
    //MARK: - 懒加载控件
    //顶部视图
    private lazy var topView: statusTopView = statusTopView()
    
    //内容标签
    lazy var contentLabel: UILabel = {
        
        let l = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        l.numberOfLines = 0
        
        //设置文本折行的宽度,若不设置这个属性,有的时候行高会不准确
        l.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        return l
    }()
    
    //底部视图
    lazy var bottomView:StatusBottomView = StatusBottomView()
    
    //配图视图
    lazy var pictureView:StatusPictureView = StatusPictureView()
    
}
